/**
 * 对称性相关类型定义
 *
 * 注意：后端使用 snake_case，但经过 axios 拦截器自动转换为 camelCase
 * 对应后端模型: server/app/models.py
 */

// ==================== 基础模型 ====================

export interface Complex {
  real: number
  imag: number
}

/**
 * 对称操作
 * 后端字段: SymmetryOperation
 * coordinate_triplet -> coordinateTriplet, ita_symbol -> itaSymbol, irrep_matrices -> irrepMatrices
 *
 * 包含完整的对称操作信息，遵循 Bilbao Crystallographic Server 的标准格式
 */
export interface SymmetryOperation {
  id: number
  coordinateTriplet: string
  matrix: number[][]
  itaSymbol: string
  seitz: string

  // 扩展字段（从 O(3) 矩阵计算得出）
  det?: number // 行列式（±1）
  axis?: number[] | null // 旋转轴 [nx, ny, nz]
  angle?: number | null // 旋转角度（弧度）
  su2?: Complex[][] | null // SU(2) 矩阵（仅对正常旋转有效，det=+1）
  irrepMatrices?: Record<string, Complex[][]> | null // 各不可约表示的矩阵
}

/**
 * 不可约表示
 * 后端字段: IrreducibleRepresentation
 * ir_label -> irLabel, matrix_refs -> matrixRefs, 等
 */
export interface IrreducibleRepresentation {
  irLabel: string
  dimension: number
  characters: (number | string)[]
  matrixRefs?: string[]
  timeReversal: boolean
  isSpinor: boolean
  source?: string
  validationStatus: 'verified' | 'computed' | 'pending'
}

/**
 * 特征标表中的不可约表示行
 * 后端字段: CharacterTableIrrep
 */
export interface CharacterTableIrrep {
  gamma: string
  mulliken: string
  characters: string[]
}

/**
 * 特征标表
 * 后端字段: CharacterTable
 * operations -> operations (Bilbao 简写符号)
 */
export interface CharacterTable {
  classes: string[]
  irreps: CharacterTableIrrep[] // 第一行可能是 Mult. 表头
  operations: string[] // Bilbao 简写符号（如 ["1", "2z", "my", "mx"]）
  note?: string
}

// ==================== 点群模型 ====================

/**
 * 点群基础信息
 * 后端字段: PointGroupBase
 * group_id -> groupId, is_crystallographic -> isCrystallographic
 *
 * 注：order 字段可以是具体值（如 2）或参数化表达式（如 'n', '2n'）
 */
export interface PointGroupBase {
  groupId: string
  schoenflies: string
  international?: string
  name: string
  order: number | string
  isCrystallographic: boolean
}

/**
 * 晶体学点群
 * 后端字段: CrystallographicPointGroup
 * crystal_system -> crystalSystem, laue_class -> laueClass
 */
export interface CrystallographicPointGroup extends PointGroupBase {
  isCrystallographic: true
  crystalSystem: string
  laueClass: string
  order: number
}

/**
 * 非晶体学点群
 * 后端字段: NonCrystallographicPointGroup
 * n_parameter -> nParameter
 */
export interface NonCrystallographicPointGroup extends PointGroupBase {
  isCrystallographic: false
  family: 'Cn' | 'Cnv' | 'Cnh' | 'Sn' | 'Dn' | 'Dnh' | 'Dnd' | 'I' | 'Ih' | 'T' | 'Td' | 'Th' | 'O' | 'Oh'
  parity: 'even' | 'odd' | null
  parameterDescription: string | null
}

export interface SubgroupInfo {
  schoenflies: string
  index: number
}

/**
 * 点群详细信息
 * 后端字段: PointGroupDetail
 */
export interface PointGroupDetail extends PointGroupBase {
  // 晶体学点群特有字段
  crystalSystem?: string
  laueClass?: string
  // 非晶体学点群特有字段
  family?: string
  parity?: 'even' | 'odd'
  parameterDescription?: string

  subgroups: SubgroupInfo[]
  generators: number[] // 生成元对应的 operation id 列表
  operations: SymmetryOperation[]
  characterTable: CharacterTable
  representations: IrreducibleRepresentation[]
  description?: string
  references: string[]
}

// ==================== 空间群模型 ====================

/**
 * Wyckoff 位置
 * 后端字段: WyckoffPosition
 * wyckoff_letter -> wyckoffLetter, site_symmetry -> siteSymmetry
 */
export interface WyckoffPosition {
  wyckoffLetter: string
  multiplicity: number
  siteSymmetry: string
  coordinates: string[]
}

/**
 * 空间群基础信息
 * 后端字段: SpaceGroupBase
 * group_id -> groupId, international_symbol -> internationalSymbol, 等
 */
export interface SpaceGroupBase {
  groupId: string
  number: number
  internationalSymbol: string
  internationalFull?: string
  schoenflies?: string
  hallSymbol?: string
  name: string
  crystalSystem: string
  pointGroup: string
}

/**
 * 空间群详细信息
 * 后端字段: SpaceGroupDetail
 */
export interface SpaceGroupDetail extends SpaceGroupBase {
  setting?: string
  latticeSystem: string
  bravaisLattice: string
  generators: string[]
  wyckoffPositions: WyckoffPosition[]
  representations: IrreducibleRepresentation[]
  subgroups: string[]
  supergroups: string[]
  latexSymbol?: string
  description?: string
  references: string[]
}

// ==================== 磁性空间群模型 ====================

/**
 * 磁性空间群基础信息
 * 后端字段: MagneticSpaceGroupBase
 * og_number -> ogNumber, og_symbol -> ogSymbol, og_type -> ogType, 等
 */
export interface MagneticSpaceGroupBase {
  groupId: string
  ogNumber: number
  ogSymbol: string
  bnsNumber?: string
  bnsSymbol?: string
  name: string
  ogType: 'Type-I' | 'Type-II' | 'Type-III' | 'Type-IV'
  parentSpaceGroup: number
}

/**
 * 磁性空间群详细信息
 * 后端字段: MagneticSpaceGroupDetail
 */
export interface MagneticSpaceGroupDetail extends MagneticSpaceGroupBase {
  unitaryOperations: SymmetryOperation[]
  antiUnitaryOperations: SymmetryOperation[]
  representations: IrreducibleRepresentation[]
  magneticPointGroup?: string
  timeReversalInvariant: boolean
  description?: string
  references: string[]
}

// ==================== 自旋空间群模型 ====================

/**
 * 自旋空间群基础信息
 * 后端字段: SpinSpaceGroupBase
 * ssg_number -> ssgNumber, international_symbol -> internationalSymbol, 等
 */
export interface SpinSpaceGroupBase {
  groupId: string
  ssgNumber: number
  internationalSymbol: string
  name: string
  parentMsg?: number
  parentSpaceGroup: number
}

/**
 * 自旋空间群详细信息
 * 后端字段: SpinSpaceGroupDetail
 */
export interface SpinSpaceGroupDetail extends SpinSpaceGroupBase {
  spinOperations: SymmetryOperation[]
  representations: IrreducibleRepresentation[]
  spinPointGroup?: string
  description?: string
  references: string[]
}

// ==================== 响应模型 ====================

/**
 * 分页响应（泛型）
 * 后端字段: PaginatedResponse
 */
export interface PaginatedResponse<T> {
  total: number
  data: T[]
}

/**
 * 点群列表响应
 * 返回晶体点群和非晶体点群的联合类型
 */
export interface PointGroupsResponse {
  data: (CrystallographicPointGroup | NonCrystallographicPointGroup)[]
  total: number
}

/**
 * 空间群列表响应
 */
export interface SpaceGroupsResponse {
  data: SpaceGroupBase[]
  total: number
}

/**
 * 磁性空间群列表响应
 */
export interface MagneticGroupsResponse {
  data: MagneticSpaceGroupBase[]
  total: number
}

/**
 * 自旋空间群列表响应
 */
export interface SpinGroupsResponse {
  data: SpinSpaceGroupBase[]
  total: number
}

// ==================== 点群树模型 ====================

/**
 * 点群树边（连接关系）
 */
export interface PointGroupTreeEdge {
  source: string
  target: string
  index: number
}

/**
 * 点群树数据响应
 */
export interface PointGroupTree {
  nodes: CrystallographicPointGroup[]
  edges: PointGroupTreeEdge[]
}

// ==================== 统计信息模型 ====================

/**
 * 数据库统计信息
 * 后端字段: Statistics
 * point_groups -> pointGroups, magnetic_space_groups -> magneticSpaceGroups, 等
 */
export interface SymmetryStatistics {
  pointGroups: {
    total: number
    crystallographic: number
    nonCrystallographic: number
  }
  spaceGroups: {
    total: number
  }
  magneticSpaceGroups: {
    total: number
    typeI: number
    typeIi: number
    typeIii: number
    typeIv: number
  }
  spinSpaceGroups: {
    total: number
  }
}
